package com.minimalist.lindi.manager;

import cn.hutool.core.bean.BeanUtil;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.basic.vo.file.FileVO;
import com.minimalist.common.entity.lindi.po.LindiSubsidyApplicationPayRecord;
import com.minimalist.common.entity.lindi.po.LindiSubsidyApplicationTownPayment;
import com.minimalist.common.entity.lindi.vo.subsidyapplicationpayrecord.LindiSubsidyApplicationPayRecordVO;
import com.minimalist.common.entity.lindi.vo.subsidyapplicationtownpayment.LindiSubsidyApplicationTownPaymentVO;
import com.minimalist.common.feign.client.basic.FileFeignClient;
import com.minimalist.common.response.R;
import com.minimalist.lindi.mapper.LindiSubsidyApplicationPayRecordMapper;
import com.minimalist.lindi.mapper.LindiSubsidyApplicationTownPaymentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 补贴申请支付通知管理器
 *
 * @author 11987
 * @since 2025-01-27
 */
@Component
public class LindiSubsidyApplicationPayNoticeManager{

    @Autowired
    private LindiSubsidyApplicationPayRecordMapper lindiSubsidyApplicationPayRecordMapper;

    @Autowired
    private LindiSubsidyApplicationTownPaymentMapper lindiSubsidyApplicationTownPaymentMapper;

    @Autowired
    private FileFeignClient fileFeignClient;

    /**
     * 根据补贴申请ID查询支付记录列表
     *
     * @param subsidyApplicationId 补贴申请ID
     * @return 支付记录VO列表
     */
    public List<LindiSubsidyApplicationPayRecordVO> getSubsidyApplicationPayRecordListBySubsidyApplicationId(Long subsidyApplicationId){
        List<LindiSubsidyApplicationPayRecord> payRecords = lindiSubsidyApplicationPayRecordMapper
                .selectPayRecordListBySubsidyApplicationId(subsidyApplicationId);
        return BeanUtil.copyToList(payRecords, LindiSubsidyApplicationPayRecordVO.class);
    }

    /**
     * 根据补贴申请支付通知ID查询镇街支付明细列表
     *
     * @param subsidyApplicationPayNoticeId 补贴申请支付通知ID
     * @return 镇街支付明细VO列表
     */
    public List<LindiSubsidyApplicationTownPaymentVO> getSubsidyApplicationTownPaymentListBySubsidyApplicationPayNoticeId(Long subsidyApplicationPayNoticeId){
        List<LindiSubsidyApplicationTownPayment> townPayments =
                lindiSubsidyApplicationTownPaymentMapper
                        .selectSubsidyApplicationTownPaymentListBySubsidyApplicationPayNoticeId(
                                subsidyApplicationPayNoticeId);
        return BeanUtil.copyToList(townPayments, LindiSubsidyApplicationTownPaymentVO.class);
    }

    /**
     * 批量添加镇街支付明细
     *
     * @param townPaymentList 镇街支付明细VO列表
     */
    public void batchAddSubsidyApplicationTownPayment(List<LindiSubsidyApplicationTownPaymentVO> townPaymentList){
        List<LindiSubsidyApplicationTownPayment> townPayments = BeanUtil.copyToList(
                townPaymentList, LindiSubsidyApplicationTownPayment.class);
        lindiSubsidyApplicationTownPaymentMapper.insertBatch(townPayments);
    }

    /**
     * 根据补贴申请支付通知ID删除镇街支付明细
     *
     * @param subsidyApplicationPayNoticeId 补贴申请支付通知ID
     */
    public void deleteSubsidyApplicationTownPaymentBySubsidyApplicationPayNoticeId(Long subsidyApplicationPayNoticeId){
        lindiSubsidyApplicationTownPaymentMapper.deleteSubsidyApplicationTownPaymentBySubsidyApplicationPayNoticeId(subsidyApplicationPayNoticeId);
    }

    /**
     * 根据补贴申请ID查询支付记录列表（分页）
     *
     * @param subsidyApplicationId 补贴申请ID
     * @param pageNum              页码
     * @param pageSize             页大小
     * @return 支付记录分页结果
     */
    public PageResp<LindiSubsidyApplicationPayRecordVO> getSubsidyApplicationPayRecordPageBySubsidyApplicationId(Long subsidyApplicationId, Integer pageNum, Integer pageSize){
        PageResp<LindiSubsidyApplicationPayRecord> pageResp =
                lindiSubsidyApplicationPayRecordMapper
                        .getSubsidyApplicationPayRecordPageBySubsidyApplicationId(
                                subsidyApplicationId, pageNum, pageSize);
        List<LindiSubsidyApplicationPayRecordVO> voList = pageResp.getRecords().stream()
                .map(record -> BeanUtil.copyProperties(record, LindiSubsidyApplicationPayRecordVO.class))
                .collect(Collectors.toList());

        // 查询文件信息
        List<Long> fileIds = voList.stream()
                .map(LindiSubsidyApplicationPayRecordVO::getFileId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());

        if (!fileIds.isEmpty()){
            try{
                R<List<FileVO>> fileResult = fileFeignClient.getFileListByFileIds(fileIds);
                if (fileResult != null && fileResult.getData() != null){
                    Map<Long, FileVO> fileMap = fileResult.getData().stream()
                            .collect(Collectors.toMap(FileVO::getFileId, file -> file));

                    voList.forEach(vo -> {
                        if (vo.getFileId() != null && fileMap.containsKey(vo.getFileId())){
                            vo.setFile(fileMap.get(vo.getFileId()));
                        }
                    });
                }
            } catch (Exception e){
                // 文件查询失败时不影响主要业务逻辑
            }
        }

        return new PageResp<>(voList, pageResp.getTotal());
    }
}